<template>
  <div class="serviceAndSupport">
    <div class="content">
      <div class="left-type">
        <div v-for="(item) in productType" :key="item.id" class="type-item" @click="chooseType(item)">
          <span class="type-item-name" :class="{ active: item.isActive }">{{ item.typeName }}</span>
        </div>
      </div>
      <div class="right-product">
        <div>
          <div class="product-box" v-if="isShow">
            <div class="product-item" v-for="item in productList" :key="item.id" @click="jumpHw(item.productUrl)">
              <h3 class="product-title">{{ item.productName }}</h3>
              <div class="product-introduction">{{ item.productIntroduction }}</div>
            </div>
          </div>
          <div class="product-box" v-else>
            <div class="product-item" v-for="item in productList" :key="item.id">
              <h3 class="product-title">{{ item.typeName }}</h3>
              <div class="product-introduction product-name-child" v-for="i in item.productList"
                @click="jumpHw(i.productUrl)">{{
                  i.productName }}</div>
            </div>
          </div>
        </div>

      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue';
import { productTypeList, productLiist, loginParameters } from '@/api/product';
import { ElMessage, ElMessageBox } from 'element-plus'
import { useRouter } from 'vue-router';
const router = useRouter();

const productType = ref([]);//类型列表
const productList = ref([]);//产品列表
const isShow = ref(true);//是否显示不含子类的盒子
// const productListChild = ref([]);//产品列表含有子类
const hwUrl = ref('');//跳转华为云链接
const isFirstLogin = ref(true);//华为云参数
const xAccountType = ref('');//华为云参数

onMounted(() => {
  getProductTypeList();
});


/**选择类型 */
const chooseType = (item) => {
  productType.value.forEach(item => item.isActive = false);
  item.isActive = true;
  getProductList(item.id);
};

/**获取产品类型列表 */
const getProductTypeList = () => {
  let data = {
    status: 1,
    params: {
      pageNum: 1,
      pageSize: 100
    }
  };
  productTypeList(data).then(res => {
    if (200 == res.code) {
      productType.value = res.rows || [];
      productType.value.forEach(item => item.isActive = false);
      if (productType.value.length > 0) {
        productType.value[0].isActive = true;
        getProductList(productType.value[0].id);
      }
    }
  })
};

/**获取产品列表 */
const getProductList = (typeId) => {
  productLiist({ typeId }).then(res => {
    if (200 == res.code) {
      productList.value = res.data;
      isShow.value = productList.value[0]?.productList ? false : true;
    }
  })
};

/**点击跳转华为云页面 */
const jumpHw = (url) => {
  loginParameters().then(res => {
    if (200 === res.code) {
      isFirstLogin.value = res.data.isFirstLogin;
      xAccountType.value = res.data.xAccountType;
      hwUrl.value = `https://auth.huaweicloud.com/authui/saml/login?xAccountType=${xAccountType.value}&isFirstLogin=${isFirstLogin.value}&service=`;
      if (isFirstLogin.value) {
        ElMessageBox.confirm(
          '您当前未绑定该产品华为云账号，请先绑定',
          '绑定云厂商',
          {
            confirmButtonText: '去绑定',
            cancelButtonText: '取消',
            type: 'warning',
          }
        )
          .then(() => {
            if (xAccountType.value) {
              router.push(`/client/cloudProduct/bindHw?xAccountType=${xAccountType.value}&toUrl=${url}`);
              // window.location.href = hwUrl.value + encodeURIComponent(url);
            }
          })
          .catch(() => {
            ElMessage({
              type: 'info',
              message: '取消绑定',
            })
          })
      } else {
        if (xAccountType.value) {
          // router.push(`/client/cloudProduct/hwProduct?xAccountType=${xAccountType.value}&toUrl=${url}`);
          window.location.href = hwUrl.value + encodeURIComponent(url);
        }
      }
    }
  })


}
</script>

<style scoped lang="less">
.serviceAndSupport {
  width: 100%;
  height: 500px;
  z-index: 3000;
  margin: 0 auto;
  display: flex;
  position: absolute;
  justify-content: center;

  .content {
    width: 100%;
    background-color: #fff;
    box-shadow: 0 2px 5px #e4e4e4;
    animation: fade_In 0.5s ease-in-out;
    display: flex;
    padding-bottom: 20px;


    .left-type {
      width: 15%;
      height: 100%;
      // overflow-x: hidden;
      overflow-y: scroll;

      &::-webkit-scrollbar {
        width: 5px;
        height: 5px;
      }

      &::-webkit-scrollbar-track {
        background: rgb(239, 239, 239);
        border-radius: 2px;
      }

      &::-webkit-scrollbar-thumb {
        background: #bfbfbf;
        border-radius: 10px;
      }

      .type-item {
        padding: 0 10px;
        margin: 30px 20px;
        cursor: pointer;

        .active {
          font-weight: 700;
          border-bottom: 2px solid #333;
        }
      }
    }

    .right-product {
      width: 85%;
      height: 100%;
      overflow-y: scroll;

      &::-webkit-scrollbar {
        width: 5px;
        height: 5px;
      }

      &::-webkit-scrollbar-track {
        background: rgb(239, 239, 239);
        border-radius: 2px;
      }

      &::-webkit-scrollbar-thumb {
        background: #bfbfbf;
        border-radius: 10px;
      }

      .product-box {
        width: 100%;
        min-height: 100px;
        display: flex;
        flex-wrap: wrap;
        padding: 0 20px;

        .product-item {
          margin: 20px 20px 0 0;
          width: 300px;
          min-height: 100px;
          display: flex;
          flex-direction: column;
          padding-left: 20px;
          cursor: pointer;

          &:hover {
            border-radius: 5px;
            box-shadow: 0 2px 5px #e4e4e4;
          }

          .product-introduction {
            margin-top: 5px;
          }

          .product-name-child {
            &:hover {
              text-decoration: underline;
            }
          }
        }
      }


    }
  }
}

@keyframes fade_In {
  0% {
    opacity: 0;
  }

  100% {
    opacity: 1;
  }
}
</style>